package com.mopoba.app;

import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.mrpoid.shequ2.R;

/**
 * 
 * @author Yichou 2013-9-22
 * 
 */
public class ThemeActivity extends FragmentActivity implements OnPageChangeListener, OnClickListener  {
	ViewPager mPager;
	TextView[] mTvTitles;
	DisplayMetrics metrics;
	ImageView imageView;
	LayoutParams params;
	Window mWindow;
	int mBgColor;
	
	static final int COLOR_DEF_BG = 0xffffe4c4;
	static final int COLOR_TITLE_FOCUS = 0xff4169e1; //0xff41691e
	static final int COLOR_TITLE = 0xffffffff;
	

	private void setBgImage(int resid) {
		mWindow.setBackgroundDrawableResource(resid);
	}
	
	private void setBg(int color) {
		mBgColor = color;
		mWindow.setBackgroundDrawable(new ColorDrawable(color));
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.tv_btn_applay) {
			setResult(mBgColor);
			finish();
		} else if (v.getId() == R.id.tv_btn_color) {
			mPager.setCurrentItem(0);
		} else if (v.getId() == R.id.tv_btn_image) {
			mPager.setCurrentItem(1);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_theme);
		
		metrics = getResources().getDisplayMetrics();
		
		mWindow = getWindow();
		setBg(COLOR_DEF_BG);
		
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		mPager.setOnPageChangeListener(this);


		mTvTitles = new TextView[2];
		mTvTitles[0] = (TextView) findViewById(R.id.tv_btn_color);
		mTvTitles[1] = (TextView) findViewById(R.id.tv_btn_image);
		mTvTitles[0].setOnClickListener(this);
		mTvTitles[1].setOnClickListener(this);
		findViewById(R.id.tv_btn_applay).setOnClickListener(this);
		imageView = (ImageView) findViewById(R.id.imageView1);
		
		params = (LayoutParams) imageView.getLayoutParams();
		
		mTvTitles[0].getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onGlobalLayout() {
				int w = mTvTitles[0].getWidth();
				
				params.width = w;
				params.leftMargin = 0;
				imageView.setLayoutParams(params);
				
				mTvTitles[0].getViewTreeObserver().removeGlobalOnLayoutListener(this);
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	static class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Fragment getItem(int arg0) {
			Fragment f = null;
			if(arg0 == 0) {
				f = new ColorsFragment();
			} else {
				f = new ImagesFragment();
			}
			
			return f;
		}
	}
	
	/**
	 * 图片选取宫格
	 * 
	 * @author Yichou 2013-9-22
	 *
	 */
	public static class ImagesFragment extends Fragment implements OnItemClickListener {
		GridView mGridView;
		Bitmap[] mBitmaps;
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			mBitmaps = new Bitmap[1];
//			mBitmaps[0] = ;
//			Bitmap tmp = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper0);
//			tmp.cre
		}
		
		@Override
		public void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			((ThemeActivity)getActivity()).setBgImage(R.drawable.wallpaper0);
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.fragment_pager_grid_theme, null);
			
			mGridView = (GridView) v.findViewById(R.id.gridView1);
			mGridView.setAdapter(new ImagesAdapter());
			mGridView.setOnItemClickListener(this);
			
			return v;
		}
		
		class ImagesAdapter extends BaseAdapter {
			LayoutInflater mInflater = LayoutInflater.from(getActivity());

			@Override
			public int getCount() {
				return 3;
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ImageView v = null;
				
				if(convertView == null) {
					convertView = mInflater.inflate(R.layout.grid_item_theme_image, null);
					v = (ImageView) convertView.findViewById(R.id.imageView1);
					convertView.setTag(v);
				} else {
					v = (ImageView)convertView.getTag();
				}
				
				v.setImageResource(R.drawable.ic_wallpaper0);
				
				return convertView;
			}
		}
	}
	
	/**
	 * 颜色获取
	 * 
	 * @author Yichou
	 *
	 */
	public static class ColorsFragment extends Fragment implements OnItemClickListener {
		GridView mGridView;
		String[] mColorNames;
		int[] mColorValues;
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			mColorNames = getResources().getStringArray(R.array.color_names);
			mColorValues = getResources().getIntArray(R.array.color_values);
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.fragment_pager_grid_theme, null);
			
			mGridView = (GridView) v.findViewById(R.id.gridView1);
			mGridView.setAdapter(new ColorsAdapter());
			mGridView.setOnItemClickListener(this);
			
			return v;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			((ThemeActivity)getActivity()).setBg(0xff000000 | mColorValues[position]);
		}
		
		class ColorsAdapter extends BaseAdapter {
			LayoutInflater mInflater = LayoutInflater.from(getActivity());

			@Override
			public int getCount() {
				return mColorValues.length;
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				TextView tv = null;
				
				if(convertView == null) {
					convertView = mInflater.inflate(R.layout.grid_item_theme_color, null);
					tv = (TextView) convertView.findViewById(R.id.textView1);
					convertView.setTag(tv);
				} else {
					tv = (TextView)convertView.getTag();
				}
				
				int c = mColorValues[position];
				tv.setTextColor(0xf0000000 | revColor(c));
				tv.setBackgroundColor(0xff000000 | c);
				tv.setText(mColorNames[position] +  "\n#" + Integer.toHexString(c));
				
				return convertView;
			}
		}
	}
	
	public static int revColor(int color) {
		int a = (color >> 24);
		int r = 0xFF - (color >> 16) & 0xFF;
		int g = 0xFF - (color >> 8) & 0xFF;
		int b = 0xFF - (color) & 0xFF;
		
		return ((a<<24)|(r<<16)|(g<<8)|b);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		params.leftMargin = (int) ((arg0 + arg1)*params.width);
		imageView.setLayoutParams(params);
	}

	int curPage = 0;
	@Override
	public void onPageSelected(int page) {
		if(curPage != page){
			mTvTitles[curPage].setTextColor(COLOR_TITLE);
			mTvTitles[page].setTextColor(COLOR_TITLE_FOCUS);

			curPage = page;
		}
	}
}
